package com.jiayang.hhotel.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.FinanceHandleModel;
import com.jiayang.hhotel.fragment.CheckOutTabFragment;
import com.jiayang.hhotel.presenter.CheckOutPresenter;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.base.XFragmentAdapter;
import cn.droidlover.xdroidmvp.custom.TitleListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 结账退房页面
 */

public class CheckOutActivity extends XActivity<CheckOutPresenter> implements TitleListener {

    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    private List<Fragment> fragmentList= new ArrayList<>();
    private XFragmentAdapter adapter;
    private int showItem;//当前显示的页面
    private FinanceHandleModel.PCashierListDtosBean model;

    @Override
    public void initData(Bundle savedInstanceState) {
        model = (FinanceHandleModel.PCashierListDtosBean) getIntent().getExtras().getSerializable(Constant.INTENT_PROJECT_KEY);

        initTitle(this,true,getString(R.string.check_out),R.color.orange_color,getString(R.string.select_all),this);
        mViewPager= (ViewPager) findViewById(R.id.viewPager);
        mTabLayout= (TabLayout) findViewById(R.id.tabLayout);

        fragmentList.clear();
        fragmentList.add(new CheckOutTabFragment(1,model));
        fragmentList.add(new CheckOutTabFragment(4,model));

        String[] titles=getResources().getStringArray(R.array.check_out_list);

        if (adapter == null) {
            adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
        }
        mViewPager.setAdapter(adapter);

        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab.getPosition()==0){//自付账单
                    showItem = 1;
                }else{//公付账单
                    showItem = 4;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_check_out;
    }

    @Override
    public CheckOutPresenter newP() {
        return new CheckOutPresenter();
    }

    @Override
    public void rightClickListener() {
        CheckOutTabFragment fragment;
        switch (showItem){
            case 1:
                fragment = (CheckOutTabFragment)fragmentList.get(0);
                if(fragment.IsSuccess()){
                    fragment.getAdapter().setIsAllSelCheck(true);
                }
                break;
            case 4:
                fragment = (CheckOutTabFragment)fragmentList.get(1);
                if(fragment.IsSuccess()){
                    fragment.getAdapter().setIsAllSelCheck(true);
                }
                break;
        }
    }
}
